<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="entityFilterFormGroup" class="tb-entity-filter">
  <mat-form-field class="mat-block" appearance="outline">
    <mat-label translate>alias.filter-type</mat-label>
    <mat-select required formControlName="type">
      <mat-option *ngFor="let type of aliasFilterTypes" [value]="type">
        {{aliasFilterTypeTranslations.get(type) | translate}}
      </mat-option>
    </mat-select>
    <mat-error *ngIf="entityFilterFormGroup.get('type').hasError('required')">
      {{ 'alias.filter-type-required' | translate }}
    </mat-error>
  </mat-form-field>
  <section class="flex flex-col" [formGroup]="filterFormGroup" [ngSwitch]="entityFilterFormGroup.get('type').value">
    <ng-template [ngSwitchCase]="aliasFilterType.singleEntity">
      <tb-entity-select required
                        appearance="outline"
                        useAliasEntityTypes="true"
                        formControlName="singleEntity">
      </tb-entity-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityList">
      <tb-entity-type-select required
                             appearance="outline"
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <tb-entity-list required
                      appearance="outline"
                      syncIdsWithDB
                      labelText="{{'entity.entity-list' | translate}}"
                      [entityType]="filterFormGroup.get('entityType').value"
                      formControlName="entityList">
      </tb-entity-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityName">
      <tb-entity-type-select required
                             appearance="outline"
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>entity.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'entity.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="entityNameFilter" required>
        <mat-error *ngIf="filterFormGroup.get('entityNameFilter').hasError('required')">
          {{ 'entity.entity-name-filter-required' | translate }}
        </mat-error>
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityType">
      <tb-entity-type-select required
                             appearance="outline"
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.stateEntity">
      <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
        <mat-label translate>alias.state-entity-parameter-name</mat-label>
        <input matInput formControlName="stateEntityParamName"
               placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
      </mat-form-field>
      <div class="tb-form-panel stroked no-padding-bottom">
        <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
        <tb-entity-select appearance="outline"
                          useAliasEntityTypes="true"
                          formControlName="defaultStateEntity">
        </tb-entity-select>
      </div>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.assetType">
      <tb-entity-subtype-list required
                              floatLabel="always"
                              appearance="outline"
                              label="{{ 'asset.asset-types' | translate }}"
                              [entityType]="entityType.ASSET"
                              formControlName="assetTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>asset.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'asset.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="assetNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.deviceType">
      <tb-entity-subtype-list required
                              floatLabel="always"
                              appearance="outline"
                              label="{{ 'device.device-types' | translate }}"
                              [entityType]="entityType.DEVICE"
                              formControlName="deviceTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>device.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'device.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="deviceNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityViewType">
      <tb-entity-subtype-list required
                              floatLabel="always"
                              appearance="outline"
                              label="{{ 'entity-view.entity-view-types' | translate }}"
                              [entityType]="entityType.ENTITY_VIEW"
                              formControlName="entityViewTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>entity-view.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'entity-view.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="entityViewNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.edgeType">
      <tb-entity-subtype-list required
                              floatLabel="always"
                              appearance="outline"
                              label="{{ 'edge.edge-types' | translate }}"
                              [entityType]="entityType.EDGE"
                              formControlName="edgeTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>edge.name-starts-with</mat-label>
        <input matInput formControlName="edgeNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.relationsQuery">
      <section class="tb-form-panel no-border no-padding">
        <div class="tb-form-panel stroked" [class.no-padding-bottom]="!filterFormGroup.get('rootStateEntity').value">
          <div class="tb-form-panel-title" translate>alias.root-entity</div>
          <mat-slide-toggle class="mat-slide" formControlName="rootStateEntity">
            {{ 'alias.root-state-entity' | translate }}
          </mat-slide-toggle>
          @if (filterFormGroup.get('rootStateEntity').value) {
            <div class="tb-form-panel no-border no-padding no-gap">
              <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
                <mat-label translate>alias.state-entity-parameter-name</mat-label>
                <input matInput formControlName="stateEntityParamName"
                       placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
              </mat-form-field>
              <div class="tb-form-panel stroked no-padding-bottom">
                <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
                <tb-entity-select appearance="outline"
                                  useAliasEntityTypes="true"
                                  formControlName="defaultStateEntity">
                </tb-entity-select>
              </div>
            </div>
          } @else {
            <tb-entity-select required
                              appearance="outline"
                              useAliasEntityTypes="true"
                              formControlName="rootEntity">
            </tb-entity-select>
          }
        </div>
        <div class="tb-form-panel stroked">
          <div class="tb-form-panel-title" translate>alias.query-options</div>
          <div class="flex flex-row gap-4">
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>relation.direction</mat-label>
              <mat-select required formControlName="direction">
                <mat-option *ngFor="let type of directionTypes" [value]="type">
                  {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
                </mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="flex-1" floatLabel="always" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>alias.max-relation-level</mat-label>
              <input matInput
                     type="number"
                     min="1"
                     step="1"
                     placeholder="{{ 'alias.unlimited-level' | translate }}"
                     formControlName="maxLevel">
            </mat-form-field>
          </div>
          <mat-slide-toggle *ngIf="filterFormGroup.get('maxLevel').value > 1 || !filterFormGroup.get('maxLevel').value"
                            class="mat-slide" formControlName="fetchLastLevelOnly">
            {{ 'alias.last-level-relation' | translate }}
          </mat-slide-toggle>
        </div>
        <div class="tb-form-panel stroked">
          <div class="tb-form-panel-title" translate>relation.relation-filters</div>
          <tb-relation-filters
            enableNotOption
            [allowedEntityTypes]="allowedEntityTypes"
            formControlName="filters">
          </tb-relation-filters>
        </div>
      </section>
    </ng-template>
    <ng-template [ngSwitchCase]="entityFilterFormGroup.get('type').value === aliasFilterType.assetSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.deviceSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.edgeSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.entityViewSearchQuery ?
                                 entityFilterFormGroup.get('type').value : ''">
      <section class="tb-form-panel no-border no-padding">
        <div class="tb-form-panel stroked" [class.no-padding-bottom]="!filterFormGroup.get('rootStateEntity').value">
          <div class="tb-form-panel-title" translate>alias.root-entity</div>
          <mat-slide-toggle class="mat-slide" formControlName="rootStateEntity">
            {{ 'alias.root-state-entity' | translate }}
          </mat-slide-toggle>
          @if (filterFormGroup.get('rootStateEntity').value) {
            <div class="tb-form-panel no-border no-padding no-gap">
              <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
                <mat-label translate>alias.state-entity-parameter-name</mat-label>
                <input matInput formControlName="stateEntityParamName"
                       placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
              </mat-form-field>
              <div class="tb-form-panel stroked no-padding-bottom">
                <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
                <tb-entity-select appearance="outline"
                                  useAliasEntityTypes="true"
                                  formControlName="defaultStateEntity">
                </tb-entity-select>
              </div>
            </div>
          } @else {
            <tb-entity-select required
                              appearance="outline"
                              useAliasEntityTypes="true"
                              formControlName="rootEntity">
            </tb-entity-select>
          }
        </div>
        <div class="tb-form-panel stroked">
          <div class="tb-form-panel-title" translate>alias.query-options</div>
          <div class="flex flex-row gap-4">
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>relation.direction</mat-label>
              <mat-select required formControlName="direction">
                <mat-option *ngFor="let type of directionTypes" [value]="type">
                  {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
                </mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="flex-1" floatLabel="always" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>alias.max-relation-level</mat-label>
              <input matInput
                     type="number"
                     min="1"
                     step="1"
                     placeholder="{{ 'alias.unlimited-level' | translate }}"
                     formControlName="maxLevel">
            </mat-form-field>
          </div>
          <mat-slide-toggle *ngIf="filterFormGroup.get('maxLevel').value > 1 || !filterFormGroup.get('maxLevel').value"
                            class="mat-slide" formControlName="fetchLastLevelOnly">
            {{ 'alias.last-level-relation' | translate }}
          </mat-slide-toggle>
        </div>
        <div class="tb-form-panel stroked no-padding-bottom">
          <div class="tb-form-panel-title" translate>relation.relation-filter</div>
          <tb-relation-type-autocomplete
            appearance="outline"
            subscriptSizing="dynamic"
            formControlName="relationType">
          </tb-relation-type-autocomplete>
          <ng-container [ngSwitch]="entityFilterFormGroup.get('type').value">
            <ng-template [ngSwitchCase]="aliasFilterType.assetSearchQuery">
              <tb-entity-subtype-list
                required
                floatLabel="always"
                appearance="outline"
                label="{{ 'asset.asset-types' | translate }}"
                [entityType]="entityType.ASSET"
                formControlName="assetTypes">
              </tb-entity-subtype-list>
            </ng-template>
            <ng-template [ngSwitchCase]="aliasFilterType.deviceSearchQuery">
              <tb-entity-subtype-list
                required
                floatLabel="always"
                appearance="outline"
                label="{{ 'device.device-types' | translate }}"
                [entityType]="entityType.DEVICE"
                formControlName="deviceTypes">
              </tb-entity-subtype-list>
            </ng-template>
            <ng-template [ngSwitchCase]="aliasFilterType.edgeSearchQuery">
              <tb-entity-subtype-list
                required
                floatLabel="always"
                appearance="outline"
                label="{{ 'edge.edge-types' | translate }}"
                [entityType]="entityType.EDGE"
                formControlName="edgeTypes">
              </tb-entity-subtype-list>
            </ng-template>
            <ng-template [ngSwitchCase]="aliasFilterType.entityViewSearchQuery">
              <tb-entity-subtype-list
                required
                floatLabel="always"
                appearance="outline"
                label="{{ 'entity-view.entity-view-types' | translate }}"
                [entityType]="entityType.ENTITY_VIEW"
                formControlName="entityViewTypes">
              </tb-entity-subtype-list>
            </ng-template>
          </ng-container>
        </div>
      </section>
    </ng-template>

  </section>
</div>
